package com.zhb.cloud.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @ClassName: Swagger2Config
 * @Description: Swagger2 的配置
 * @author: zhb1316783262@163.com
 * @date: 2019年1月16日下午7:01:25
 */
@Slf4j
@Configuration
@EnableSwagger2
@Profile({ "local", "dev", "test1", "test2" })
public class Swagger2Config {
	@Value("${spring.profiles.active}")
	private String active;

	public @Bean Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any()).build();
	}

	/**
	 * @methodname: apiInfo
	 * @description: 基本信息展示
	 * @author: zhb1316783262@163.com
	 * @date: 2019年1月16日下午7:05:54
	 * @return: ApiInfo
	 * @return
	 */
	private ApiInfo apiInfo() {
		log.info("当前环境是:" + active);
		// 根据当前的profiles指定各个环境的访问地址
		String swaggerUrl = "http://localhost:8080";
		switch (active) {
		case "test1":
			swaggerUrl = "http://localhost:8080";
			break;
		case "test2":
			swaggerUrl = "http://localhost:8080";
			break;
		case "dev":
			swaggerUrl = "http://localhost:8080";
			break;
		default:
			swaggerUrl = "http://localhost:8080";
			break;
		}
		return new ApiInfoBuilder().title("XXXX API").description("XXXX的API").termsOfServiceUrl(swaggerUrl)
				.contact(new Contact("朱宏斌", swaggerUrl, "zhb1316783262@163.com")).version("0.0.1-SNAPSHOT").build();
	}
}
